<template>
  <page-container>
    <search-form
      v-model="searchForm"
      :searchColumns="searchColumns"
      :searchOptions="{ menuSpan: 4 }"
      @on-search="handleSearch">
      <template v-slot:newBtn>
        <el-button
          type="success"
          @click="handleExport"
          >导出</el-button
        >
      </template>
    </search-form>

    <avue-table
      :dataList="dataList"
      :loading="loading"
      :page="page"
      :tableColumns="tableColumns"
      :tableOptions="{ menuWidth: 80 }"
      @onPageChange="onPageChange">
      <template v-slot:accountLog="{ row }"
        ><el-button
          type="text"
          @click="handleViewForm(row)"
          >查看</el-button
        ></template
      >
    </avue-table>
  </page-container>
</template>

<script>
import tableMixins from '@/mixins/table'
import { deepClone } from '@/utils'
import { mapGetters } from 'vuex'
import { getUserList } from '@/api/order'
import { searchColumns, tableColumns } from './config/table-config'
import { accountLogColumns } from './config/dialog-config'

export default {
  mixins: [tableMixins],
  data() {
    return {
      dataList: [],
      addDataList: [],
      childList: [],
      searchForm: {
        dateRange: [this.$startStamp(new Date()), this.$endStamp(new Date())],
      },
      editForm: {},
    }
  },
  computed: {
    ...mapGetters(['commonData', 'countryList', 'channelList']),
    userList() {
      return this.commonData?.userList ?? []
    },
    searchColumns,
    tableColumns,
    accountLogColumns,
  },
  methods: {
    fetchMethod: getUserList,

    // 查询参数格式化
    formatSearch() {
      const searchParams = deepClone(this.searchForm)
      const [start, end] = this.searchForm.dateRange
      searchParams.start = start && this.$dayjs(this.$startStamp(start)).unix()
      searchParams.end = end && this.$dayjs(this.$endStamp(end)).unix()
      delete searchParams.dateRange
      return searchParams
    },
    handleViewForm(row) {
      const parm = deepClone(row)
      this.$dialogForm.show({
        title: '账号日志',
        data: parm,
        width: '60%',
        option: {
          span: 24,
          submitBtn: false,
          column: this.accountLogColumns,
        },
      })
    },
    // 点击导出
    async handleExport() {
      const allData = await this.queryAllData(this.page.total)

      const colums = this.tableColumns.filter(item => {
        return item.prop !== 'Id'
      })

      const originList = deepClone(allData)
      const exportDataList = originList.map((item, index) => {
        Object.entries(item).forEach(([key, value]) => {
          if (key === '$index') {
            item[key] = index + 1
          }
          if (key === 'register_time') {
            item[key] = this.$formatTime(value)
          }
          const k = ['balance', 'bonus_consume', 'coins', 'coins_consume', 'sum_consume']
          if (k.includes(key)) {
            item[key] = String(value / 10000)
          }
        })
        return item
      })

      this.$Export.excel({
        title: '用户列表数据',
        columns: colums,
        data: exportDataList,
      })
    },
    async queryAllData(pageSize) {
      this.addDataList = []

      const searchParams = this.formatSearch()
      const queryParams = {
        ...searchParams,
        page: this.page.currentPage,
        page_size: pageSize,
      }

      const { data } = await this.fetchMethod(queryParams)
      return data?.list ?? []
    },
  },
  mounted() {
    this.handleSearch()
  },
}
</script>

<style lang="scss" scoped></style>
